package com.inforich.web.controller.applyreport;

import cn.hutool.core.collection.CollUtil;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inforich.applyreport.domain.RubbishApplyReport;
import com.inforich.applyreport.domain.RubbishApplyReportCell;
import com.inforich.applyreport.service.IRubbishApplyReportService;
import com.inforich.common.annotation.Log;
import com.inforich.common.core.controller.BaseController;
import com.inforich.common.enums.BusinessType;
import com.inforich.common.utils.poi.ExcelUtil;

/**
 * 垃圾申报量统计Controller
 *
 * @author liujie
 * @date 2022-11-21
 */
@RestController
@RequestMapping("/report/applyreport")
public class RubbishApplyReportController extends BaseController {

  @Autowired
  private IRubbishApplyReportService rubbishApplyReportService;

  /**
   * 查询垃圾申报量统计列表
   */
  @PreAuthorize("@ss.hasPermi('report:applyreport:list')")
  @GetMapping("/list")
  public Page<RubbishApplyReportCell> list(Page<RubbishApplyReportCell> page,
      RubbishApplyReport rubbishApplyReport) {
    Map<String, Object> map = beanToMap(rubbishApplyReport);
    rubbishApplyReportService.pageRubbishApplyReportCell(page, map);
    RubbishApplyReportCell totalCell = createTotalCell(page.getRecords());
    if (CollUtil.isNotEmpty(page.getRecords())) {
      page.getRecords().add(totalCell);
    }
    return page;
  }

  /**
   * 导出垃圾申报量统计列表
   */
  @PreAuthorize("@ss.hasPermi('report:applyreport:export')")
  @Log(title = "垃圾申报量统计", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, RubbishApplyReport rubbishApplyReport) {
    Map<String, Object> map = beanToMap(rubbishApplyReport);
    List<RubbishApplyReportCell> list = rubbishApplyReportService.listRubbishApplyReportCell(map);
    RubbishApplyReportCell totalCell = createTotalCell(list);
    list.add(totalCell);
    ExcelUtil<RubbishApplyReportCell> util = new ExcelUtil<RubbishApplyReportCell>(
        RubbishApplyReportCell.class);
    util.exportExcel(response, list, "垃圾申报量统计");
  }

  /**
   * 合计
   *
   * @param cellList List<RubbishApplyReportCell>
   * @return RubbishApplyReportCell
   */
  private RubbishApplyReportCell createTotalCell(List<RubbishApplyReportCell> cellList) {
    RubbishApplyReportCell totalCell = new RubbishApplyReportCell();
    if (cellList == null || cellList.size() == 0) {
      return totalCell;
    }
    BigDecimal disposalAreaTotal = new BigDecimal("0");
    BigDecimal disposalWeightTotal = new BigDecimal("0");
    BigDecimal disposalPlanWeightTotal = new BigDecimal("0");
    for (RubbishApplyReportCell cell : cellList) {
      disposalAreaTotal = disposalAreaTotal.add(
          new BigDecimal(Optional.ofNullable(cell.getDisposalArea()).orElse("0")));
      disposalWeightTotal = disposalWeightTotal.add(
          new BigDecimal(Optional.ofNullable(cell.getDisposalWeight()).orElse("0")));
      disposalPlanWeightTotal = disposalPlanWeightTotal.add(
          new BigDecimal(Optional.ofNullable(cell.getDisposalPlanWeight()).orElse("0")));
    }

    totalCell.setProjectName("合计:");
    totalCell.setDisposalArea(disposalAreaTotal.toPlainString());
    totalCell.setDisposalWeight(disposalWeightTotal.toPlainString());
    totalCell.setDisposalPlanWeight(disposalPlanWeightTotal.toPlainString());

    return totalCell;
  }
}
